/*
 * Balloon.cpp
 *
 *  Created on: 2012/07/05
 *      Author: weihe
 */

#include "Balloon.h"
#include <iostream>
#include <gl/glut.h>
#include "utility.h"

using namespace std;

namespace object {

int Balloon::count = 0;

const Material Balloon::colorMaterial[] = { //
		Material(Color4f(0.9, 0.9, 0.0, 0.7)), //Yellow
		Material(Color4f(0.3, 0.9, 0.2, 0.7)), //Green
		Material(Color4f(0.0, 0.8, 0.8, 0.7)), //LightBlue
		Material(Color4f(0.0, 0.2, 0.5, 0.7)), //DarkBlue
		Material(Color4f(0.9, 0.9, 0.0, 0.7)), //Red
		Material(Color4f(0.9, 0.6, 0.5, 0.7)), //Pink
		};

const Material Balloon::scoreMaterial[] = { //
		Material(Color4f(0.8, 0.5, 0.2)), //Copper
		Material(Color4f(0.8, 0.8, 0.8)), //Silver
		Material(Color4f(0.8, 0.7, 0.2)), //Gold
		Material(Color4f(0.8, 0.8, 0.8)), //Silver
		Material(Color4f(0.8, 0.7, 0.2)), //Gold
		};

const Material Balloon::lineMaterial(0.1, 0.1, 0.1);

const Balloon::ColorType Balloon::ColorTypeList[] = { Yellow, Green, LightBlue,
		DarkBlue, Red, Pink, };
const Balloon::ScoreType Balloon::ScoreTypeList[] = { Copper, Silver, Gold,
		TreasureSilver, TreasureGold, };

Balloon::Balloon(Camera *camera, Vector4f position, ColorType colorType,
		ScoreType scoreType) :
		Drawable(camera), exploded(false), colorType(colorType), scoreType(
				scoreType), position(position)
{
	speed = nextDouble() * 8 + 1;
	count++;
	switch (scoreType) {
	case Copper:
		score = 10;
		break;
	case Silver:
		score = 15;
		break;
	case Gold:
		score = 20;
		break;
	case TreasureSilver:
		score = 30;
		break;
	case TreasureGold:
		score = 40;
		break;
	}
}

Balloon::~Balloon()
{
}

void Balloon::draw()
{
	static int ballId = -1, scoreId;
	if (ballId == -1) {
		ballId = glGenLists(6);
		scoreId = glGenLists(5);
		for (int i = 0; i < 6; i++) {
			glNewList(ballId + i, GL_COMPILE);
			{
				glPushMatrix();
				{
					Balloon::colorMaterial[i].setupMaterial(GL_FRONT);
					glScalef(0.8, 1.1, 0.8);
					glutSolidSphere(0.8, 30, 30);
				}
				glPopMatrix();
				//line
				Balloon::lineMaterial.setupMaterial(GL_FRONT);
				glBegin(GL_LINE_LOOP);
				{
					glVertex3d(0, -0.48, 0);
					glVertex3d(0, -2.48, 0);
				}
				glEnd();
			}
			glEndList();
		}
		for (int i = 0; i < 5; i++) {
			glNewList(scoreId + i, GL_COMPILE);
			{
				glPushMatrix();
				{
					if (i <= 2) {
						//coin 1,5,10
						glTranslatef(0, -2.48, 0);
						Balloon::scoreMaterial[i].setupMaterial(GL_FRONT);
						glScalef(1, 1, 0.3);
						glutSolidSphere(0.3, 30, 30);
					} else {
						//treasure
						glTranslatef(0, -2.48, 0);
						glScalef(0.4, 0.4, 0.4);
						Balloon::scoreMaterial[i].setupMaterial(GL_FRONT);
						glutSolidIcosahedron();
					}
				}
				glPopMatrix();
			}
			glEndList();
		}
	}
	glPushMatrix();
	{
		glTranslatef(position.x, position.y, position.z);
		glCallList(ballId + colorType);
		glCallList(scoreId + scoreType);
	}
	glPopMatrix();
}

} /* namespace object */
